package cn.enited.devtools.apiaggregator.entity;

import cn.enited.devtools.apiaggregator.anno.QueryField;
import cn.enited.devtools.apiaggregator.repo.group.AllMethodGroup;
import cn.enited.devtools.apiaggregator.repo.group.FindListGroup;
import cn.enited.devtools.apiaggregator.repo.group.FindOneGroup;
import cn.enited.devtools.apiaggregator.repo.group.FindPageGroup;
import cn.enited.devtools.apiaggregator.utils.IdUtils;
import cn.hutool.core.date.DateUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;

import java.io.Serializable;
import java.util.Date;

@Data
@EqualsAndHashCode
@ToString
@Accessors(chain = true)
public abstract class BaseEntity implements Serializable {
    private static final long serialVersionUID = -5736740716439225444L;

    @Id
    private ObjectId id;

    @QueryField(groups = {AllMethodGroup.class})
    private Long bizId;

    @QueryField(groups = {FindOneGroup.class, FindListGroup.class, FindPageGroup.class})
    private Date createDate;

    @QueryField(groups = {FindOneGroup.class, FindListGroup.class, FindPageGroup.class})
    private Date updateDate;

    private Boolean delFlag;

    public void preCreate() {
        this.bizId = IdUtils.snowflakeId();
        this.updateDate = this.createDate = DateUtil.date();
        this.delFlag = false;
    }

    public void preUpdate() {
        this.updateDate = DateUtil.date();
    }

    public void preDelete() {
        this.updateDate = DateUtil.date();
        this.delFlag = true;
    }
}
